package com.fg.cloud.framework.system;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.Resource;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.tuple.TwoTuple;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.entry.system.MenuEntity;
import com.fg.cloud.manager.service.system.MenuService;

/**
 * Description:系统菜单处理
 * 
 * @author shaoqiping
 * @date 2017年8月17日下午1:57:57
 */
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController {
	
	@Autowired
	private MenuService menuService;

	/**
	 * 厂商平台--导航菜单
	 * 
	 * @param request
	 * @param response
	 */
	@RequiresRoles(UserType.FACTORY)
	@RequestMapping(value = "/factory", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void findMenuByFactory(HttpServletRequest request, HttpServletResponse response) {
		FactoryUserVo user = getCurrentFactoryUser();
		if (null == user) {
			doResult(response, true, StatusCode.LOGINOUT, null, ResultType.LOGINOUT);
			return;
		}
		try {
			List<Resource> leftMuen = menuService.findByUserId(user.getId(), user.getUserType());
			List<Resource> setupMenu = menuService.findByUserId(user.getId(), 3);
			TwoTuple<List<Resource>, List<Resource>> data = new TwoTuple<List<Resource>, 
					List<Resource>>(null == leftMuen ? new ArrayList<Resource>() : leftMuen,
					null == setupMenu ? new ArrayList<Resource>() : setupMenu);
			doResult(response, true, StatusCode.FINDSUCCESS, data, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * Description:查询菜单功能列表数据，完整展示全部内容
	 * 
	 * @param request
	 * @param response
	 * @param classify
	 *            菜单类型：0=平台菜单；1=厂家菜单；2=经销商菜单 3=系统菜单
	 * @author shaoqiping
	 * @date 2017年8月17日下午1:52:54
	 */
	@RequestMapping(value = "/findList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, Integer classify) {
		try {
			if (classify == null || classify < 0) {
				doResult(response, true, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, menuService.findList(classify), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:新增菜单的信息
	 * 
	 * @param request
	 * @param response
	 * @param menuEntity
	 * @author shaoqiping
	 * @date 2017年8月16日下午2:48:27
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response, MenuEntity menuEntity) {
		try {
			// 条件判断
			if (menuEntity == null || StringUtils.isEmpty(menuEntity.getName())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "菜单名称不能为空！");
				return;
			}
			boolean flag = menuService.insertSelective(menuEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:更改某个菜单的信息
	 * 
	 * @param request
	 * @param response
	 * @param menuEntity
	 * @author shaoqiping
	 * @date 2017年8月16日下午3:38:29
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, MenuEntity menuEntity) {
		try {
			// 条件判断
			if (menuEntity == null || menuEntity.getId() == null || menuEntity.getId() <= 0) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要修改的菜单！");
				return;
			}
			boolean flag = menuService.updateByPrimaryKeySelective(menuEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:上下移动控件
	 * 
	 * @param request
	 * @param response
	 * @param menuEntity
	 * @author shaoqiping
	 * @date 2017年8月17日上午10:05:06
	 */
	@RequestMapping(value = "/move", method = RequestMethod.POST)
	@ResponseBody
	public void move(HttpServletRequest request, HttpServletResponse response, Long id, Integer seq, Long changeId,
			Integer changeSeq) {
		try {
			if (id == null || LongUtils.isEmpty(id) || changeId == null || LongUtils.isEmpty(changeId) || seq == null
					|| changeSeq == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要移动的菜单！");
				return;
			}
			boolean flag = menuService.move(id, seq, changeId,changeSeq);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null,
					flag ? ResultType.SAVE_SUCCESS : "已到达顶部或底部");
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:删除某个菜单的信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author shaoqiping
	 * @date 2017年8月16日下午4:23:56
	 */
	@RequestMapping(value = "/deletes")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id",required=true) Long id,
			@RequestParam(value = "number",required=true) String number) {
		try {
			if (id == null || LongUtils.isEmpty(id)||number==null||StringUtils.isEmpty(number)) {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.CHOOSE_INFO);
				return;
			}
			boolean flag = menuService.deletes(id,number);
			if (flag) {
				doResult(response, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

}
